﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxTreeView;

namespace EntryExit
{
    public partial class TempCardDetailsSaving : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                datetimepicker.Value = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
                datetimepicker5.Value = "00:00" ;
                Employee SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
                if (SelectedEmployee != null)
                {
                    lblSelectedEmployee.Text = SelectedEmployee.EmployeeName;
                }
            }

        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.Master is MainMaster)
            {
                ((MainMaster)this.Master).MasterTreeViewNodeSelected += TempCardDetailsSaving_MasterTreeViewNodeSelected;
            }
        }
        void TempCardDetailsSaving_MasterTreeViewNodeSelected(object source, TreeViewNodeEventArgs e)
        {
            Employee SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
            if (SelectedEmployee != null)
            {
                lblSelectedEmployee.Text = SelectedEmployee.EmployeeName;
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblError.Visible = false;
            lblError.Text = "";
            if (datetimepicker5.Value == ""|| datetimepicker5.Value == "00:00")
            {
                lblError.Visible = true;
                lblError.Text = "Error : Please enter total working hours";
                return;
            }
            if (SelectedEmployeeLPN.Trim() == "")
            {
                lblError.Visible = true;
                lblError.Text = "Error : Please select an employee";
                return;
            }
            if (txtTempCardNo.Text.Trim() == "")
            {
                lblError.Visible = true;
                lblError.Text = "Error : Please enter temp card No";
                return;
            }
           
                Employee SelectedEmployee = EmployeeManagerContext.GetEmployee(SelectedEmployeeLPN);
                if (SelectedEmployee != null)
                {
                    lblSelectedEmployee.Text = SelectedEmployee.EmployeeName;
                }
            DateTime From = DateTime.ParseExact(datetimepicker.Value, "dd/MM/yyyy HH:m", null);
            if (From.CompareTo(DateTime.Now) > 0)
            {
                lblError.Visible = true;
                lblError.Text = "Error : Cannot apply for future date";
                return;
            }
            string[] s = datetimepicker5.Value.Split(':');
            TimeSpan To = new TimeSpan(Convert.ToInt16(s[0]), Convert.ToInt16(s[1]), 0);
            DateTime EndDate = From.AddHours(To.TotalHours);
            DataTable dt = EmployeeManagerContext.UpdateTempCartUseage(SelectedEmployeeLPN, From, EndDate, To, txtTempCardNo.Text, CurrentEmployee.LPN.ToString());
            if (dt.Rows.Count > 0)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "SADASD", "alert('" + dt.Rows[0]["msg"].ToString() + "');", true);
            }
        }

        protected void ddlDepartments_SelectedIndexChanged(object sender, EventArgs e)
        {
            // ddlEmployee
            //DataTable dt = EmployeeManagerContext.GetEmployees(ddlDepartments.SelectedValue.ToString());
            //ddlEmployee.DataSource = dt;
            //ddlEmployee.DataTextField = "EmployeeName";
            //ddlEmployee.DataValueField = "LPN";
            //ddlEmployee.DataBind();
            //if(ddlEmployee.Items.Count>0)
            //ddlEmployee.SelectedIndex = 0;
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            datetimepicker.Value = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
            datetimepicker5.Value = "00:00";
            txtTempCardNo.Text = "";
        }
    }
}